﻿Shader "Example01/09扭曲扰动"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ContortTex("ContortTex",2D) ="bump"{}
        _Contort("Contort",Range(-1,1)) = 1
        _Speed("Speed",Range(0,10)) =1
    }
    SubShader
    {
        Tags
        { 
            // 渲染顺序 - 透明
            "Queue" = "Transparent"
            // 着色器替换 - 透明
            "RenderType" = "Transparent"
            // 面板预览形状 - 片<默认显示的是球形的>
            "PreviewType" ="Plane"
        }
        
        Cull Off		// 剔除 关闭
        Lighting Off	// 灯光 关闭
        ZWrite Off		// 深度缓冲 关闭
        // Blend SrcAlpha One                  //Add模式
        Blend SrcAlpha OneMinusSrcAlpha     //Blended模式
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1; // 声明UV2
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _ContortTex;
            float4 _ContortTex_ST;
            float _Contort;
            float _Speed;
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv =TRANSFORM_TEX(v.uv,_MainTex);
                o.uv2 =TRANSFORM_TEX(v.uv,_ContortTex); // 采样第二套UV
                o.uv2.x +=_Time.x * _Speed;             // 偏移UV
                return o;
            }
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 contortTex = tex2D(_ContortTex, i.uv2);
                i.uv -= contortTex.rg * 0.1 * _Contort; // 扭曲打乱
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}